# @Time    : 2018/5/30 17:46
# @Author  : Jame
from lib import common
from interface  import student_interface,common_interface


student_info={
    'name':None,
    'role':'student'
}

def student_register():
    print('学生注册>>>')
    while True:
        name = input('请输入注册的用户名:').strip()
        password = input('请输入注册的密码:').strip()
        conf_password = input('请再次输入注册的密码:').strip()
        if password == conf_password:
            flag, msg = student_interface.student_register_interface(name, password)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print('两次密码不一致，请重新输入.')



def student_login():
    print('学生登陆>>>')
    while True:
        name = input('请输入用户名:').strip()
        if student_info['name'] == name:
            print('%s 已登陆，请勿重复重登录...' % student_info['name'])
            break

        password = input('请输入密码:').strip()
        flag, msg = common_interface.login(name,password,student_info['role'])
        if flag:
            print(msg)
            student_info['name'] = name
            break
        else:
            print(msg)



@common.auth_type(student_info['role'])
def student_choose_school():
    while True:
        flag1,msg1 = common_interface.chose_school()
        if flag1:
            school_name=msg1
            flag2,msg2=student_interface.student_select_school_interface(student_info['name'],school_name)
            if flag2:
                print(msg2)
                break
            else:
                print(msg2)
        else:
            print(msg1)



@common.auth_type(student_info['role'])
def student_choose_course():
    while True:
        flag1,msg1 = common_interface.chose_course()
        if flag1:
            course=msg1
            flag2,msg2=student_interface.student_select_course_interface(student_info['name'],course)
            if flag2:
                print(msg2)
                break
            else:
                print(msg2)
        else:
            print(msg1)



@common.auth_type(student_info['role'])
def student_check_score():
    print('查看成绩:')

    flag,msg=student_interface.student_check_score_interface(student_info['name'])
    if flag:
        print(msg)

    else:
        print(msg)


@common.auth_type(student_info['role'])
def student_pay_cost():
    print('欢迎 %s 交学费:' %student_info['name'])
    flag,msg = student_interface.pay_cost_interface(student_info['name'])
    if flag:
        print(msg)
    else:
        print(msg)

@common.auth_type(student_info['role'])
def modify_password():
    print('修改我的密码')
    while True:
        newpassword=input('请输入新密码:').strip()
        conf_newpassword=input('再次确认新密码:').strip()
        if newpassword==conf_newpassword:
            flag,msg=student_interface.modify_passwod_interface(student_info['name'],newpassword)
            if flag:
                print(msg)
                student_info['name']=None
                student_login()
                break
            else:
                print(msg)
                break
        else:
            print('两次输入的密码不一致，请重新输入..')


func_dic = {
    '1': student_register,
    '2': student_login,
    '3': student_choose_school,
    '4': student_choose_course,
    '5': student_check_score,
    '6': student_pay_cost,
    '7': modify_password

}

def student_view():
    while True:
        print('''
        1、注册
        2、登录
        3、选择校区
        4、选择课程       
        5、查看成绩
        6、交学费 
        7、修改我的密码
        ''')
        choice = input('please choice (q 退出)>>:').strip()
        if choice == 'q': break
        if choice not in func_dic:
            print('您输入的不在范围内,请重新输入.')
            continue
        else:
            func_dic[choice]()
